package com.TeacherInfoServer.controller;

import com.TeacherInfoServer.common.Result;
import com.TeacherInfoServer.listener.TemporaryPositionListener;
import com.TeacherInfoServer.pojo.DTO.AddTemporaryPositionDTO;
import com.TeacherInfoServer.pojo.DTO.SearchDTO.TemporaryPositionSearchDTO;
import com.TeacherInfoServer.pojo.DTO.UpdateTemporaryPositionDTO;
import com.TeacherInfoServer.pojo.excelImport.TemporaryPositionExcel;
import com.TeacherInfoServer.pojo.vo.TemporaryPositionListVo;
import com.TeacherInfoServer.common.service.TemporaryPositionService;
import com.alibaba.excel.EasyExcel;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/s1/temporary_position")
public class TemporaryPositionController {

    @Autowired
    private TemporaryPositionService temporaryPositionService;
    @Autowired
    private TemporaryPositionListener temporaryPositionListener;

    @PostMapping("/addOne")
    public Result<Map<String, Integer>> addOne(@RequestBody @Valid AddTemporaryPositionDTO addTemporaryPositionDTO){
        return temporaryPositionService.addOne(addTemporaryPositionDTO);
    }

    @PostMapping("/addBatch")
    public Result<Map<String,Integer>> addBatch(@RequestBody MultipartFile file) throws IOException{
        temporaryPositionListener.initList();
        EasyExcel.read(file.getInputStream(), TemporaryPositionExcel.class,temporaryPositionListener).sheet().doRead();
        Map<String,Integer> map = new HashMap<>();
        map.put("record",temporaryPositionListener.getRecord());
        return Result.success(map);
    }

    @GetMapping(value = "/getList",params = {"pageNumber", "pageSize"})
    public Result<TemporaryPositionListVo> getList(@RequestParam Long pageNumber, @RequestParam Long pageSize){
        return temporaryPositionService.getList(pageNumber,pageSize);
    }
    @Operation(summary = "模糊查询获取教师挂职信息列表",description = "模糊查询分页获取教师挂职信息列表")
    @PostMapping("/searchList")
    public Result<Map<String,Object>> searchList(@RequestBody TemporaryPositionSearchDTO temporaryPositionSearchDTO){
        return Result.success("获取教师挂职列表成功",temporaryPositionService.searchTemporaryPositionList(temporaryPositionSearchDTO));
    }

//    @GetMapping(value = "getOneList", params = {"pageNumber", "pageSize","teacherId"})
//    public Result<TemporaryPositionListVo> getOneList(@RequestParam Long pageNumber,@RequestParam Long pageSize,@RequestParam String teacherId){
//        return temporaryPositionService.getOneList(pageNumber,pageSize,teacherId);
//    }

    @GetMapping(value = "/export",params = "fileName")
    public void export(HttpServletResponse response, @RequestParam String fileName) throws IOException {
       temporaryPositionService.export(response, fileName);
    }

    @DeleteMapping(value = "/delete",params = "ids")
    public Result<Map<String, Integer>> deleteBatch(@RequestParam Integer[] ids){
        return temporaryPositionService.deleteBatch(ids);
    }

    @PutMapping("/update")
    public Result<Map<String, Integer>> updateOne(@RequestBody @Valid UpdateTemporaryPositionDTO updateTemporaryPositionDTO){
        return temporaryPositionService.updateOne(updateTemporaryPositionDTO);
    }
}
